package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Staff;
import com.three.mapper.StaffMapper;
import com.three.service.StaffService;
import com.three.vo.R;
import com.three.vo.StaffInfoVo;
import com.three.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class StaffImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private HttpSession session;
    @Override
    @Transactional//事务
    public R loginByNameAndPwd(StaffVo staffVo) {
        //检查账号是否为空
        if(staffVo.getStaffname()==null||staffVo.getStaffname().equals("")){
            System.out.println(staffVo.getStaffname());
            return R.error("账号不能为空!");
        }
        //检查密码是否为空
        if(staffVo.getPassword()==null||staffVo.getPassword().equals("")){
            return R.error("密码不能为空!");
        }
        //检查账号密码是否正确
        Staff staff = staffMapper.loginByNameAndPwd(staffVo.getStaffname(), staffVo.getPassword());
        if(staff==null){
            return R.error("账号或密码错误!");
        }
        //若账号密码正确，判断账号是否被禁用,只有在职的账号才能登陆
        if(staff.getState()!=1){
            return R.error("该账号已冻结或该用户已离职! ! !");
        }
        //没有被禁用，保存并返回账号信息
        session.setAttribute("staff",staff);
        return R.ok(staff);
    }

    //获取不是店长的所有人员工信息
    @Override
    public R selectStaff() {
        List<Staff> staff = staffMapper.getStaff();
        return R.ok(staff);
    }



    //  获取当前登陆人的所有信息
    @Override
    public R getStaffInfo11() {
        Staff staff = (Staff) session.getAttribute("staff");
        //System.out.println(staff);
        if(staff==null){
            return R.error("请先登陆");
        }else{
            Staff staff1 = staffMapper.selectAllStaff(staff);
            //System.out.println(staff1);
            return R.ok(staff1);
        }
    }


    //根据deptid部门id查询该部该部门下的所有员工
    @Override
    public R selectByDeptId(Integer deptid,Integer currentPage02,Integer pageSize02) {
        //开启分页
        PageHelper.startPage(currentPage02,pageSize02);
        List<Staff> staff = staffMapper.selectStaffByDeptId(deptid);
        PageInfo<Staff> pageInfo = new PageInfo<>(staff);
        return R.ok(pageInfo);
    }

    @Override
    public R getNowStaff() {
        Staff staff = (Staff) session.getAttribute("staff");
        //通过staffid查询当前登陆的员工和店铺
        Staff staff1 = staffMapper.selectStaffandBranch(staff.getStaffid());
        return R.ok(staff1);
    }

    //修改员工信息
    @Override
    public R updateStaff(Staff staff) {
        //System.out.println(staff);
        int i = staffMapper.updateByPrimaryKeySelective(staff);
        if (i > 0){
            return R.ok();
        }else {
            return R.error();
        }
    }

}
